package com.laizhenghua.wxstub.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.laizhenghua.wxstub.constant.AppConstant;
import com.laizhenghua.wxstub.service.MessageService;
import org.apache.tomcat.util.buf.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: laizhenghua
 * @date: 2022/8/31 21:25
 */
@Service("messageService")
public class MessageServiceImpl implements MessageService {
    private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(MessageServiceImpl.class);
    @Autowired
    private RestTemplate restTemplate;

    @Override
    public String callbackNotify(String signature, String timestamp, String nonce, String echostr) {
        log.info("signature=" + signature);
        log.info("timestamp=" + timestamp);
        log.info("nonce=" + nonce);
        log.info("echostr=" + echostr);
        return "receive success";
    }

    @Override
    public JSONObject getWeatherInfo(String city) {
        Map<String, Object> params = new HashMap<>();
        params.put("appid", AppConstant.TQ_APP_ID);
        params.put("appsecret", AppConstant.TQ_APP_SECRET);
        params.put("city", city);
        params.put("unescape", 1);
        return restTemplate.getForObject(AppConstant.TQ_APP_API_URL + "?appid={appid}&appsecret={appsecret}&city={city}&unescape={unescape}",
                JSONObject.class, params);
    }

    @Override
    public String getLoveContent() {
        Map<String, Object> params = new HashMap<>();
        params.put("token", AppConstant.AL_API_TOKEN);
        params.put("format", "json");
        try {
            Thread.sleep(2000);
        } catch (Exception e) {
            log.error(e);
        }
        JSONObject response = restTemplate.getForObject(AppConstant.AL_API_QH_URL + "?token={token}&format={format}", JSONObject.class, params);
        if (response == null) {
            return null;
        }
        log.info(response);
        JSONObject data = response.getObject("data", JSONObject.class);
        return data.getString("content");
    }

    @Override
    public String getYqList(String province, String city) {
        Map<String, Object> params = new HashMap<>();
        params.put("token", AppConstant.AL_API_TOKEN);
        params.put("province", province);
        params.put("city", city);
        JSONObject response = restTemplate.getForObject(AppConstant.AL_API_YQ_URL + "?token={token}&province={province}&city={city}",
                JSONObject.class, params);
        if (response == null) {
            return null;
        }
        // 高风险
        JSONObject data = response.getObject("data", JSONObject.class);
        if (data == null) {
            return null;
        }
        JSONArray yqList = data.getObject("high_list", JSONArray.class);
        if (yqList.size() == 0) {
            // 中风险
            yqList = response.getObject("middle_list", JSONArray.class);
        }
        JSONObject content = yqList.getJSONObject(0);
        JSONArray communityList = content.getObject("communitys", JSONArray.class);
        // List<String> list = communityList.toJavaList(String.class);
        String community = StringUtils.join(communityList, ',');
        String areaName = content.getString("area_name");
        String note = String.format("呜呜~出门注意避开[%s]（%s）区域哦！", community, areaName);
        return note;
    }

    @Override
    public JSONArray getWbHost(Integer size) {
        Map<String, Object> params = new HashMap<>();
        params.put("access-key", AppConstant.CODER_UTIL_ACCESS_KEY);
        params.put("secret-key", AppConstant.CODER_UTIL_SECRET_KEY);
        params.put("size", size);
        JSONObject response = restTemplate.getForObject(AppConstant.CODER_UTIL_BASE_URL + "api/resou/v1/weibo?access-key={access-key}&secret-key={secret-key}&size={size}",
                JSONObject.class, params);
        if (response == null) {
            return null;
        }
        JSONArray data = response.getObject("data", JSONArray.class);
        return data;
    }

    @Override
    public JSONArray getCsdnHost(Integer size) {
        Map<String, Object> params = new HashMap<>();
        params.put("access-key", AppConstant.CODER_UTIL_ACCESS_KEY);
        params.put("secret-key", AppConstant.CODER_UTIL_SECRET_KEY);
        params.put("size", size);
        JSONObject response = restTemplate.getForObject(AppConstant.CODER_UTIL_BASE_URL + "api/resou/v1/csdn?access-key={access-key}&secret-key={secret-key}&size={size}",
                JSONObject.class, params);
        if (response == null) {
            return null;
        }
        JSONArray data = response.getObject("data", JSONArray.class);
        return data;
    }

    @Override
    public String getWyyContent() {
        Map<String, Object> params = new HashMap<>();
        params.put("token", AppConstant.AL_API_TOKEN);
        try {
            Thread.sleep(1000);
        } catch (Exception e) {
            log.error(e);
        }
        JSONObject response = restTemplate.getForObject(AppConstant.AL_API_COMMENT_URL + "?token={token}", JSONObject.class, params);
        log.info(JSON.toJSONString(response));
        if (response == null) {
            return null;
        }
        JSONObject data = response.getObject("data", JSONObject.class);
        return String.format("《%s》%s", data.getString("title"), data.getString("comment_content"));
    }

}
